package com.fengba.nanzhi.tabbar01;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.Indicators.PagerIndicator;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.daimajia.slider.library.Tricks.ViewPagerEx;
import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.tabbar03.SubmitOrder;
import com.fengba.nanzhi.tabbar03.SubmitOrder_Dh;
import com.fengba.nanzhi.utils.Function;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;


/**
 * Created by duan on 2018/4/24.
 */

public class GoodInfo02 extends Activity {

    //屏幕的宽高
    private int screenWidth;
    private int screenHeight;
    //返回按钮
    private ImageButton back_btn;
    //背景界面
    private RelativeLayout mLayout;
    //状态栏
    private View statusBar;
    //标题栏
    private FrameLayout navigation_title;
    //滚动视图
    private SliderLayout mDemoSlider;
    //good数据
    private JSONObject goodObj;
    //good名字
    private TextView good_name;
    //good价格
    private TextView good_price;
    //原始good价格
    private TextView yuan_good_price;
    //规则说明
    private TextView rulescomment;
    //商品详情
    private WebView webview;
    //倒计时
    private TextView timer_txt;
    //我要抽
    private Button switch_submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dmy_goodinfo02);

        bindViews();
        initBackgroundColor();
        setNavigationHeight();
        setSliderHeight();
        initData();
    }

    /**
     * 绑定所有的控件
     */
    private void bindViews(){
        //界面宽高
        screenWidth      = Function.getScreenWidth(this);
        screenHeight     = Function.getScreenHeight(this);
        //返回按钮
        back_btn         = findViewById(R.id.back_btn);
        //自己的layout
        mLayout          = findViewById(R.id.self_activity);
        //状态栏
        //statusBar      = findViewById(R.id.view_statusbar);
        navigation_title = findViewById(R.id.navigation_title);
        //滚动
        mDemoSlider      = findViewById(R.id.slider);
        //商品名字
        good_name        = findViewById(R.id.good_name);
        //商品价格
        good_price       = findViewById(R.id.good_price);
        //规则说明
        rulescomment     = findViewById(R.id.rulescomment);
        //商品详情
        webview          = findViewById(R.id.webview);
        //倒计时
        timer_txt        = findViewById(R.id.timer_txt);
        //我要抽按钮
        switch_submit         = findViewById(R.id.switch_submit);

        //返回上一页
        back_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //我要抽
        switch_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String good = getIntent().getStringExtra("good");
                //Log.e("eea",good);
                Intent intent = new Intent(GoodInfo02.this, SubmitOrder_Dh.class);
                intent.putExtra("good", good.toString());
                startActivity(intent);
            }
        });
    }

    /**
     * 初始化背景页颜色
     */
    private void initBackgroundColor(){
        mLayout.setBackgroundColor(Color.parseColor("#F7F7FC"));//使用颜色的16进制类型值
    }

    /**
     * 初始化背景页颜色
     */
    private void setNavigationHeight(){
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(0,0);
        params.width = screenWidth;
        params.height = Function.dip2px(this,44);
        params.topMargin = Function.getStatusBarHeight(this);
        navigation_title.setLayoutParams(params);
    }

    /**
     * 设置滚动图片的宽高
     */
    private void setSliderHeight(){
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(0,0);
        params.width = screenWidth;
        params.height = screenWidth;
        mDemoSlider.setLayoutParams(params);
    }


    private void initData(){

        String good = getIntent().getStringExtra("good");
        try {
            JSONObject myJson = new JSONObject(good.toString());

            ///////////////////////////////////////////////////商品图片
            JSONObject goodinfo = (JSONObject) myJson.get("goods");
            String[] good_images = goodinfo.get("images").toString().split(",");
            HashMap<String,String> urlMaps = new HashMap<>();
            for(int i=0;i<good_images.length;i++){
                urlMaps.put(String.valueOf(i+1), good_images[i]);
            }
            for(String name : urlMaps.keySet()){
                TextSliderView textSliderView = new TextSliderView(this);
                View v = textSliderView.getView();
                textSliderView
                        .description(name)//描述
                        .image(urlMaps.get(name))//image方法可以传入图片url、资源id、File
                        .setScaleType(BaseSliderView.ScaleType.Fit)//图片缩放类型
                        .setOnSliderClickListener(onSliderClickListener);//图片点击
                textSliderView.bundle(new Bundle());
                textSliderView.getBundle().putString("extra",name);//传入参数
                mDemoSlider.addSlider(textSliderView);//添加一个滑动页面
            }

            mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Accordion);//滑动动画
            //mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);//默认指示器样式
            mDemoSlider.setCustomIndicator((PagerIndicator)findViewById(R.id.custom_indicator2));//自定义指示器
            mDemoSlider.setCustomAnimation(new DescriptionAnimation());//设置图片描述显示动画
            mDemoSlider.setDuration(4000);//设置滚动时间，也是计时器时间
            mDemoSlider.addOnPageChangeListener(onPageChangeListener);
            ///////////////////////商品名字
            good_name.setText(myJson.get("goods_name").toString());
            ///////////////////////商品价格
            good_price.setText(myJson.get("credit").toString());
            ////////////////////////规则说明
            rulescomment.setText(myJson.get("rulescomment").toString());
            ///////////////////////商品详情
            webview.loadData(goodinfo.get("details").toString(), "text/html", "UTF-8");
            //////////////////////倒计时
            timer_txt.setText("限量"+myJson.get("left_number").toString()+"件");
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    private BaseSliderView.OnSliderClickListener onSliderClickListener=new BaseSliderView.OnSliderClickListener() {
        @Override
        public void onSliderClick(BaseSliderView slider) {
            Toast.makeText(GoodInfo02.this,slider.getBundle().get("extra") + "",
                    Toast.LENGTH_SHORT).show();
        }
    };

    //页面改变监听
    private ViewPagerEx.OnPageChangeListener onPageChangeListener=new ViewPagerEx.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

        @Override
        public void onPageSelected(int position) {
            //Log.e("ansen", "Page Changed: " + position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {}
    };

    /**
     * 将毫秒转化为 分钟：秒 的格式
     *
     * @param millisecond 毫秒
     * @return
     */
    public String formatTime(long millisecond) {
        int hour;//小时
        int minute;//分钟
        int second;//秒数
        hour   = (int) ((millisecond / 1000) / 60 / 60 );
        minute = (int) ((millisecond / 1000) / 60 % 60);
        second = (int) ((millisecond / 1000) % 60);
        if (minute < 10) {
            if (second < 10) {
                return hour+":"+"0" + minute + ":" + "0" + second;
            } else {
                return hour+":"+"0" + minute + ":" + second;
            }
        }else {
            if (second < 10) {
                return hour+":"+minute + ":" + "0" + second;
            } else {
                return hour+":"+minute + ":" + second;
            }
        }
    }

//    /**
//     * 初始化状态栏高度
//     */
//    private void initStatusbarHeight(){
//        RelativeLayout.LayoutParams status_view_params = new RelativeLayout.LayoutParams(0,0);
//        status_view_params.width = screenWidth;
//        status_view_params.height = Function.getStatusBarHeight(this);
//        statusBar.setLayoutParams(status_view_params);
//    }

}
